package com.dataseek.iot.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.device.entity.PdtList;
import com.dataseek.iot.device.mapper.PdtListMapper;
import com.dataseek.iot.device.service.DeviceListService;
import com.dataseek.iot.device.vo.DeviceListVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.service.IDServiceHelper;
import com.dataseek.iot.toolkit.mybatis.entity.DeviceList;
import com.dataseek.iot.toolkit.mybatis.mapper.DeviceListMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @discription:
 * @author: Freya.Yan
 * @date: 2022/1/11
 */
@Service
@Slf4j
public class DeviceListServiceImpl implements DeviceListService {
    @Autowired
    private IDServiceHelper idServiceHelper;

    @Autowired
    private DeviceListMapper deviceListMapper;

    @Autowired
    private PdtListMapper pdtListMapper;

    @Override
    public DeviceListVo queryDeviceList(String pdtCode, String ctgCode, String deviceName, Integer num, Integer size) {

        DeviceListVo pdtListVo = new DeviceListVo();
        QueryWrapper<DeviceList> qw = new QueryWrapper<>();
        if (deviceName != null && !deviceName.isEmpty()) {
            qw.like("device_name", deviceName);
        }
        if (pdtCode != null && !pdtCode.isEmpty()) {
            qw.like("pdt_name", pdtCode);
        }
        if (ctgCode != null && !ctgCode.isEmpty()) {
            qw.like("ctg_code", ctgCode);
        }
        IPage<DeviceList> page = new Page<>(num, size);
        List<DeviceList> mapList = deviceListMapper.selectPage(page, qw).getRecords();
        pdtListVo.setPage(num);
        pdtListVo.setSize(size);
        pdtListVo.setTotal(page.getTotal());
        pdtListVo.setList(mapList);
        return pdtListVo;
    }

    //新增部门信息
    @Override
    public Integer insertDeviceList(DeviceList deviceList) {
        int resultCode = 0;
        long id = idServiceHelper.nextId();
        deviceList.setId(id);
        String code = deviceList.getDeviceCode();
        String name = deviceList.getDeviceName();
        String pdtCode=deviceList.getPdtCode();
        QueryWrapper<DeviceList> qw = new QueryWrapper<>();
        qw.eq("device_code",code);
        qw.or();
        qw.eq("device_name", name);
        Integer count = deviceListMapper.selectCount(qw);
        if (count == 0) {
           // pdtAttributeDic.setTenantCode(tenantCode);
            QueryWrapper<PdtList> pdtqw = new QueryWrapper<>();
            pdtqw.eq("pdt_code",pdtCode);
            PdtList onePdt=pdtListMapper.selectOne(pdtqw);
            deviceList.setPdtName(onePdt.getPdtName());
            deviceList.setCtgCode(onePdt.getCtgCode());
            deviceList.setCtgName(onePdt.getCtgName());
            deviceListMapper.insert(deviceList);
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;

    }

    @Override
    public Integer updateDeviceList(DeviceList deviceList) {
        int resultCode = deviceListMapper.updateById(deviceList);
        return resultCode;
    }

    @Override
    public Integer deleteDeviceList(List<Long> ids) {
        Integer resultCode = deviceListMapper.deleteBatchIds(ids);;

        return resultCode ;
    }

    @Override
    public DeviceList queryOneDevice(long deviceId) {
        return deviceListMapper.selectById(deviceId);
    }

    @Override
    public DeviceList queryDeviceByCode(String deviceCode) {
        QueryWrapper<DeviceList> qw=new QueryWrapper();
        qw.eq("device_code",deviceCode);
        List<DeviceList> deviceLists= deviceListMapper.selectList(qw);
        if(deviceLists!=null&&deviceLists.size()>0) {
            DeviceList deviceList = deviceLists.get(0);
            return deviceList;
        }else {
            return null;
        }
    }

}
